package com.hilihili.transcode;

import com.hilihili.config.TranscodeConfig;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 普通cpu转码
 *
 * @author machi
 * @date 2024/01/31
 */
public class NoneCommand extends AbstractTranscodeCommand {

    private TranscodeConfig transcodeConfig;


    public NoneCommand(TranscodeConfig transcodeConfig) {
        this.transcodeConfig = transcodeConfig;
    }

    @Override
    protected List<String> getTranscodeCommand(String ffmpeg) {
        // 构建命令
        //
        //ffmpeg  -i input.mp4
        // -map 0:0 -map 0:1 -map 0:0 -map 0:1 -map 0:0 -map 0:1 -map 0:0 -map 0:1 -s:v:0 1920x1080 -b:v:0 1800k -s:v:1 1280x720 -b:v:1 1200k -s:v:2 858x480 -b:v:2 750k -s:v:3 640x360 -b:v:3  550k  -c:a copy -var_stream_map "v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:480p v:3,a:3,name:360p"
        // -master_pl_name index.m3u8 -f hls -hls_time 10
        // -hls_playlist_type vod -hls_list_size 0 -hls_segment_filename "%v/%06d.ts" %v/index.m3u8
        List<String> commands = new ArrayList<>();
        commands.add(ffmpeg);
        commands.add("-i");
        // 源文件
        commands.add(getSource());
        commands.add("-map");
        commands.add("0:0");
        commands.add("-map");
        commands.add("0:1");
        commands.add("-map");
        commands.add("0:0");
        commands.add("-map");
        commands.add("0:1");
        commands.add("-map");
        commands.add("0:0");
        commands.add("-map");
        commands.add("0:1");
        commands.add("-map");
        commands.add("0:0");
        commands.add("-map");
        commands.add("0:1");
        commands.add("-s:v:0");
        commands.add("1920x1080");
        commands.add("-b:v:0");
        commands.add("1800k");
        commands.add("-s:v:1");
        commands.add("1280x720");
        commands.add("-b:v:1");
        commands.add("1200k");
        commands.add("-s:v:2");
        commands.add("858x480");
        commands.add("-b:v:2");
        commands.add("750k");
        commands.add("-s:v:3");
        commands.add("640x360");
        commands.add("-b:v:3");
        commands.add("550k");

        commands.add("-c:a");
        commands.add("copy");
        commands.add("-var_stream_map");
        commands.add("\"v:0,a:0,name:1080p v:1,a:1,name:720p v:2,a:2,name:480p v:3,a:3,name:360p\"");
        // 视频编码为H264
        commands.add("-master_pl_name");
        commands.add("index.m3u8");

        commands.add("-hls_key_info_file");
        // 指定密钥文件路径
        commands.add(getKeyInfo());
        commands.add("-hls_time");
        // ts切片大小
        commands.add(transcodeConfig.getTsSeconds());
        commands.add("-hls_playlist_type");
        // 点播模式
        commands.add("vod");
        commands.add("-hls_list_size");
        commands.add("0");
        commands.add("-hls_segment_filename");
        if (StringUtils.hasText(transcodeConfig.getCutStart())) {
            commands.add("-ss");
            // 开始时间
            commands.add(transcodeConfig.getCutStart());
        }
        if (StringUtils.hasText(transcodeConfig.getCutEnd())) {
            commands.add("-to");
            // 结束时间
            commands.add(transcodeConfig.getCutEnd());
        }
        // 生成m3u8文件
        commands.add("\"%v/%06d.ts\"");
        commands.add("%v/index.m3u8");
        return commands;


    }
}
